#!/usr/bin/env python


from __future__ import print_function
import subprocess as sp
import shlex as sx
import os 
import multiprocessing as mp
from Queue import Queue as Q
import argparse

def checkIfBinary(fileCmdOutput, scripts=False):
    if 'executable' in fileCmdOutput:
        if ('script' in fileCmdOutput):
            if scripts:
                return True
            return False
        return True
    return False
def strings(full_path):
    try:
        command = sx.split('file {0}'.format(full_path))
        ps1 = sp.Popen(command, stdout=sp.PIPE, stderr=sp.PIPE)
        output, error = ps1.communicate()
        if checkIfBinary(output):
            command = sx.split('strings {0}'.format(full_path))
            ps2 = sp.Popen(command, stdout=sp.PIPE, stderr=sp.PIPE)
            if ps2.stdout:
                print('binary w/strings: {0}'.format(full_path))
        #print(ps2.stdout.read(), end=' ')
    except Exception, e:
        pass
if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('directory', help='the root directory to start the search in')
    args = parser.parse_args()
    pool = mp.Pool(5)
    for root, dirs, fyles in os.walk(args.directory):
        for fyle in fyles:
            full_path = os.path.join(root, fyle)
            pool.apply_async(strings, args=(full_path,))
    pool.close()
    pool.join()

